import addressUs from "../consumptionComm/addressUs/addressUs";
import { scrollMixin } from "../../../utils/mixin";
export default {
  components: {
    addressUs
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      usClass: true, //修改的样式
      goodsId: null,
      storeName: "",
      longitude: "", //经纬度
      latitude: "",
      timer: "",
      storeListByGoods: [],
      reply: [],
      isLoadMore: true,
      page: 1,
      totalPage: 0,
      total: 0
    };
  },
  activated() {
    if ((this.$route.query.goodsId ?? "") === "" && (this.$route.query.longitude ?? "") === "" && (this.$route.query.latitude ?? "") === "") {
      return;
    }
    this.goodsId = Number(this.$route.query.goodsId);
    this.longitude = this.$route.query.longitude;
    this.latitude = this.$route.query.latitude;
    this.page = 1;
    this.getStoreListByGoods();
  },
  methods: {
    searchChange(value) {
      this.storeName = value;
      // this.setData({ storeName: detail });
      // 防抖
      if (this.timer) clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.debounce(this.getStoreListByGoods());
      }, 500);
    },
    debounce(fn, delay) {
      let last, deferTimer;
      return function(args) {
        let that = this;
        let _args = arguments;
        let now = +new Date();
        if (last && now < last + delay) {
          clearTimeout(deferTimer);
          deferTimer = setTimeout(function() {
            last = now;
            fn.apply(that, _args);
          }, delay);
        } else {
          last = now;
          fn.apply(that, _args);
        }
      };
    },
    handleScroll() {
      //滚动事件触发
      if (this.getScrollTop() + this.getClientHeight() + 105 > this.getScrollHeight()) {
        //此处发起请求
        if (this.isLoadMore) {
          this.getMoreData();
        } else {
          console.log("没有更多数据");
        }
      }
    },
    getStoreListByGoods() {
      let json = {
        goods_id: this.goodsId,
        store_name: this.storeName,
        longitude: this.longitude,
        latitude: this.latitude,
        page: this.page
      };
      $http.post("plugin.coupon-store.frontend.store.getStoreListByGoods", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.isLoadMore = true;
            this.totalPage = res.last_page;
            if (!this.totalPage) {
              this.page = 0;
            }
            this.storeListByGoods = res.data;
            this.total = res.total;
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.totalPage) {
        return;
      } else {
        this.page += +1;
        let json = {
          goods_id: this.goodsId,
          store_name: this.storeName,
          longitude: this.longitude,
          latitude: this.latitude,
          page: this.page
        };
        $http.post("plugin.coupon-store.frontend.store.getStoreListByGoods", json, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              let list = res.data;
              this.isLoadMore = true;
              this.storeListByGoods = [...this.storeListByGoods, ...list];
              this.total = res.total;
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
    }
  }
};
